package com.jinly.ryder.common.web;

import com.jinly.ryder.common.auth.annotation.Auth;
import com.jinly.ryder.common.auth.model.AuthRole;
import com.jinly.ryder.common.auth.model.AuthType;
import com.jinly.ryder.common.service.SystemSettingService;
import com.jinly.ryder.common.service.model.BaseEnum;
import com.jinly.ryder.common.service.model.SystemSetting;
import com.jinly.ryder.common.service.model.setting.BaseInfoSetting;
import lombok.RequiredArgsConstructor;
import lombok.extern.slf4j.Slf4j;
import org.springframework.web.bind.annotation.*;

import java.util.HashMap;
import java.util.Map;

@Slf4j
@RestController
@RequestMapping
@RequiredArgsConstructor
public class SystemController {

    private final SystemSettingService systemSettingService;
    private Map<String, BaseEnum[]> baseEnumMap = new HashMap<>();

    @GetMapping("sysInfo")
    public BaseInfoSetting sysInfo() {
        BaseInfoSetting baseInfoSetting = null;
        try {
            baseInfoSetting = systemSettingService.getSettingItem(BaseInfoSetting.class);
        } catch (Exception e) {
            baseInfoSetting = new BaseInfoSetting();
        }
        return baseInfoSetting;
    }


    @PostMapping("saveSystemSetting")
    @Auth(type = AuthType.MANAGE, role = AuthRole.ADMIN, value = "admin")
    public void saveSystemSetting(@RequestBody SystemSetting systemSetting) {
        systemSettingService.saveSetting(systemSetting);
    }


    @GetMapping("getSystemSetting")
    @Auth(type = AuthType.MANAGE, role = AuthRole.ADMIN, value = "admin")
    public SystemSetting getSystemSetting() {
        return systemSettingService.getSetting();
    }
}
